//logs.js
const baseUrl = require('../../config').baseUrl
const app = getApp()

Page({
    data: {
        isBackstage: false, // 是否后台
        showSelectBox: false, // 显示单位选择列表
        selectImg: "images/select.png",
        selectedImg: "images/selected.png",
        selectCompanyName: '', // 选择的单位名
        selectCompanyId: '', // 选择的单位名
        addCompanyName: '', // 手动添加的单位名称
        animation: null,
        selectdataList: [],//单位选择列表数据
        scrollTop: 0,//滚动距离
        avatarUrl: ''
    },
    onLoad () {
        this.animation = wx.createAnimation()
        this.setData({
            avatarUrl: wx.getStorageSync('userInfo').avatarUrl
        })
        // 获取单位信息
        if (app.globalData.account && app.globalData.account != '') {
            this.getCompany(app.globalData.account)
        } else {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.accountCallback = account => {
                this.getCompany(account)
            }
        }
    },
    onHide: function () {
        this.setData({
            showSelectBox: false,
            isBackstage: true
        })
    },
    onShow () {
        if (this.data.isBackstage) {
            this.setData({
                isBackstage: false
            })
            this.getCompany()
        }
    },
    /**
     *  获取单位列表
     */
    getCompany (account) {
        let This = this
        wx.request({
            url: baseUrl,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                data: JSON.stringify({
                    business: "GetDeptInfoList",
                    data: {
                        account: account? account : app.globalData.account
                    }
                })
            },
            method: 'POST',
            success (res) {
                if (res.data.code === 0) {
                    This.setData({
                        selectdataList: res.data.data
                    })
                } else {
                    wx.showToast({
                        title: res.data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            fail: (error) => {
                wx.showToast({
                    title: error.data.info,
                    icon: 'none',
                    duration: 1000
                })
            }
        })
    },
    /**
     * 跳转到个人中心
     */
    gotouser: function () {
        wx.switchTab({
            url: '/pages/userinfo/userinfo',
        })
    },
    gotoCheckDetail: function () {
        if (this.data.selectCompanyName != '') {
            wx.navigateTo({
                url: `/pages/checkInDetail/index?companyId=${this.data.selectCompanyId}`
            })
        } else {
            wx.showToast({
                title: '请选择要登记的单位',
                icon: 'none',
                duration: 2000
            })
        }

    },
    showSelectBoxFn () {
        this.animation.rotate(90).step({
            duration: 200
        })
        this.setData({ animation: this.animation.export() }, () => {
            this.setData({
                showSelectBox: !this.data.showSelectBox
            })
        })
    },
    /**
     * 单位选择
     */
    seleckedCompany (e) {
        let currentTargetCompany = e.currentTarget.dataset.company
        let selectCompanyId = e.currentTarget.dataset.companyid
        this.setData({
            "selectCompanyName": currentTargetCompany ? currentTargetCompany : this.data.addCompanyName,
            selectCompanyId: selectCompanyId,
            "showSelectBox": false
        })
    },
    //单位添加
    addCompany (e) {
        this.setData({
            'addCompanyName': e.detail.value
        })
    },
    /**
     * 输入要添加单位的名字
     */
    inputAddCompanyName (e) {
        let This = this
        if (this.data.addCompanyName === '') {
            wx.showToast({
                title: '请输入添加的单位名称',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        wx.request({
            url: baseUrl,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                data: JSON.stringify({
                    business: "SaveDeptInfo",
                    data: {
                        account: app.globalData.account,
                        deptname: this.data.addCompanyName,
                        deptid: ''
                    }
                })
            },
            method: 'POST',
            success (res) {
                if (res.data.code === 0) {
                    wx.showToast({
                        title: '添加成功',
                        icon: 'none',
                        duration: 2000
                    })
                    This.getCompany(app.globalData.account)
                    This.setData({
                        'addCompanyName': ""
                    })
                } else {
                    wx.showToast({
                        title: res.data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            fail: (error) => {
                wx.showToast({
                    title: error.data.info,
                    icon: 'none',
                    duration: 1000
                })
            }
        })
    }
})
